{
 "cells": [
  {
   "cell_type": "raw",
   "id": "6c6f4521",
   "metadata": {},
   "source": [
    "DataFrame 是一个表格型的数据结构，它含有一组有序的列，每列可以是不同的值类型（数值、字符串、布尔型值）。DataFrame 既有行索引也有列索引，它可以被看做由 Series 组成的字典（共同用一个索引）。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07df5ba1",
   "metadata": {},
   "source": [
    "```python\n",
    "构造方法\n",
    "pandas.DataFrame( data, index, columns, dtype, copy)\n",
    "```\n",
    "\n",
    "\n",
    "    data：一组数据(ndarray、series, map, lists, dict 等类型)。\n",
    "\n",
    "    index：索引值，或者可以称为行标签。\n",
    "\n",
    "    columns：列标签，默认为 RangeIndex (0, 1, 2, …, n) 。\n",
    "\n",
    "    dtype：数据类型。\n",
    "\n",
    "    copy：拷贝数据，默认为 False。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "42942e8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/anaconda3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3444: FutureWarning: Could not cast to float64, falling back to object. This behavior is deprecated. In a future version, when a dtype is passed to 'DataFrame', either all columns will be cast to that dtype, or a TypeError will be raised\n",
      "  exec(code_obj, self.user_global_ns, self.user_ns)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Site</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Google</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Runoob</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Wiki</td>\n",
       "      <td>13.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Site   Age\n",
       "0  Google  10.0\n",
       "1  Runoob  12.0\n",
       "2    Wiki  13.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 创建实例\n",
    "# 使用列表创建\n",
    "import pandas as pd\n",
    "\n",
    "data = [['Google',10],['Runoob',12],['Wiki',13]]\n",
    "\n",
    "pd.DataFrame(data,columns=['Site','Age'],dtype=float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9b6b3d87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Site</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Google</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Runoob</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Wiki</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Site  Age\n",
       "0  Google   10\n",
       "1  Runoob   12\n",
       "2    Wiki   13"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(data,columns=['Site','Age'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e4ef3252",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Site</th>\n",
       "      <th>Age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Google</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Runoob</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Wiki</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Site  Age\n",
       "0  Google   10\n",
       "1  Runoob   12\n",
       "2    Wiki   13"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用 ndarrays 创建\n",
    "data = {'Site':['Google', 'Runoob', 'Wiki'], 'Age':[10, 12, 13]}\n",
    "pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a2ffdf0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5</td>\n",
       "      <td>10</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a   b     c\n",
       "0  1   2   NaN\n",
       "1  5  10  20.0"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用字典创建\n",
    "data = [{'a': 1, 'b': 2},{'a': 5, 'b': 10, 'c': 20}]\n",
    "pd.DataFrame(data)\n",
    "# 没有对应的部分数据为 NaN。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8060306f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "calories    420\n",
      "duration     50\n",
      "Name: 0, dtype: int64\n",
      "calories    380\n",
      "duration     40\n",
      "Name: 1, dtype: int64\n",
      "   calories  duration\n",
      "0       420        50\n",
      "1       380        40\n",
      "      calories  duration\n",
      "day1       420        50\n",
      "day2       380        40\n",
      "day3       390        45\n",
      "calories    380\n",
      "duration     40\n",
      "Name: day2, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "data = {\n",
    "  \"calories\": [420, 380, 390],\n",
    "  \"duration\": [50, 40, 45]\n",
    "}\n",
    "# 数据载入到 DataFrame 对象\n",
    "df = pd.DataFrame(data)\n",
    "# 返回第一行\n",
    "print(df.loc[0])\n",
    "# 返回第二行\n",
    "print(df.loc[1])\n",
    "\n",
    "# 返回第一行和第二行\n",
    "print(df.loc[[0, 1]])\n",
    "\n",
    "# 指定索引值\n",
    "df = pd.DataFrame(data, index = [\"day1\", \"day2\", \"day3\"])\n",
    "print(df)\n",
    "\n",
    "# 指定索引。使用 loc 属性返回指定索引\b对应到某一行\n",
    "print(df.loc[\"day2\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "10bb7165",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "calories    380\n",
      "duration     40\n",
      "Name: day2, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(df.loc[\"day2\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "050cceaa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
